//  import * as types from './mutation-types'
import businessConfig from '../../../api/config/business-config'
import * as globalTypes from '../../../mutation-types'
// import {m7Language, getCache} from '@/utils/m7Utils'

/**
 * 查询账户工单
 * @returns [*]
 */
export const queryAccountBusinessFlow = ({commit, state, rootState, dispatch}, data) => {
  return businessConfig.queryAccountBusinessFlow(data)
      .then(response => {
        if (response.success) {
          return response
        }
      })
      .catch(() => {
        commit(globalTypes.SET_ERROR, 'message.default_tips')
      })
}
/**
 * 查询某个工单的所有工单步骤
 * @param data.flow_id
 * @returns [*]
 */
export const queryBusinessFlowSteps = ({commit, state, rootState, dispatch}, data) => {
  return businessConfig.queryBusinessFlowSteps(data)
      .then(response => {
        return response
      })
      .catch(() => {
        commit(globalTypes.SET_ERROR, 'message.default_tips')
      })
}
/**
 * 创建工单
 */
export const addBusinessFlow = ({commit, state, rootState, dispatch}, data) => {
  return businessConfig.addBusinessFlow(data)
    .then(response => {
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 创建工单步骤
 */
export const addBusinessFlowStep = ({commit, state, rootState, dispatch}, data) => {
  return businessConfig.addBusinessFlowStep(data)
    .then(response => {
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 创建开始步骤
 */
export const addBusinessStartStepFlow = ({commit, state, rootState, dispatch}, data) => {
  return businessConfig.addBusinessStartStepFlow(data)
    .then(response => {
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 创建工单，并创建开始步骤
 */
export const addBusinessFlowAndFirstStep = ({commit, state, rootState, dispatch}, data) => {
  return businessConfig.addBusinessFlow(data)
    .then(response => {
      if (response.success) {
        let step = {
          _id: response.data._id,
          name: '开始步骤',
          ctime: '',
          ctype: 'min',
          ownernotifyUser: '',
          notifyUser: '',
          isBegin: true
        }
        // 添加步骤
        return businessConfig.addBusinessFlowStep(step).then((res) => {
          if (res.success) {
            // 设置初始步骤
            let firstStep = {
              _id: res.data._id,
              flow_id: response.data._id,
              name: '开始步骤',
              ctime: '',
              ctype: 'min',
              ownernotifyUser: '',
              notifyUser: '',
              isBegin: true
            }
            return businessConfig.updateBusinessStepInfo(firstStep).then((res) => {
              if (res.success) {
                return response
              }
            })
          }
        })
      }
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 更新工单流信息
 */
export const updateBusinessFlowInfo = ({commit, state, rootState, dispatch}, data) => {
  return businessConfig.updateBusinessFlowInfo(data)
    .then(response => {
      if (response.success) {
        return response
      }
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 查询某个工单步骤的详细信息
 * @param data._id
 * @returns {*}
 */
export const queryBusinessFlowStep = ({commit, state, rootState, dispatch}, data) => {
  return businessConfig.queryBusinessFlowStep(data)
      .then(response => {
        return response
      })
      .catch(() => {
        commit(globalTypes.SET_ERROR, 'message.default_tips')
      })
}
/**
 * 查询某个工单的所有字段
 * @param data.flow_id
 * @returns {*}
 */
export const queryAllBusinessFlowFields = ({commit, state, rootState, dispatch}, data) => {
  return businessConfig.queryAllBusinessFlowFields(data)
    .then(response => {
      if (response.success) {
        return response
      }
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 更新某个工单步骤的详细信息
 * @param Object
 * @returns {*}
 */
export const updateBusinessStepInfo = ({commit, state, rootState, dispatch}, data) => {
  return businessConfig.updateBusinessStepInfo(data)
    .then(response => {
      if (response.success) {
        return response
      }
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
export const updateBusinessStepAPI = ({commit, state, rootState, dispatch}, data) => {
  return businessConfig.updateBusinessStepAPI(data)
    .then(response => {
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
export const submitBusinessFlowApply = ({commit, state, rootState, dispatch}, data) => {
  return businessConfig.submitBusinessFlowApply(data)
    .then(response => {
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}

/*
* 添加配置工单接口：
* */
export const submitConfigBusinessFlowApply = ({commit, state, rootState, dispatch}, data) => {
  return businessConfig.submitConfigBusinessFlowApply(data)
    .then(response => {
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/*
* 查询服务工单接口：
* */
export const queryConfigBusiness = ({commit, state, rootState, dispatch}, data) => {
  return businessConfig.queryConfigBusiness(data)
    .then(response => {
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/*
* 删除服务工单接口：
* */
export const removeCnofigBusiness = ({commit, state, rootState, dispatch}, data) => {
  return businessConfig.removeCnofigBusiness(data)
    .then(response => {
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/*
* 停用流程
* */
export const changeStatusBusinessFlow = ({commit, state, rootState, dispatch}, data) => {
  return businessConfig.changeStatusBusinessFlow(data)
    .then(response => {
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/*
* @param {String} data._id  工单步骤id
* @param {Array} data.stepFields 工单步骤布局
* */
export const updateBusinessStepFields = ({commit, state, rootState, dispatch}, data) => {
  return businessConfig.updateBusinessStepFields(data)
    .then(response => {
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 添加步骤动作
 * @param data
 * @param data._id  //步骤id
 * @param data.name  //动作名称
 * @param data.jumpTo  //执行动作后转入
 * @param data.actionRole  //动作角色
 * @returns {*}
 */
export const addBusinessStepAction = ({commit, state, rootState, dispatch}, data) => {
  return businessConfig.addBusinessStepAction(data)
    .then(response => {
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 动作界面保存配置
 * @param data
 * @param data._id  //步骤id
 * @param data.actionId  //动作id
 * @param data.update  {}
 * @param data.update.name //动作名称
 * @param data.update.jumpTo //执行动作后转入
 * @param data.update.actionRole //动作角色 []
 * @param data.update.actionFields[].cols //行
 * @param data.update.actionFields[].cols[].fields //列
 * @param data.update.actionFields[].cols[].fields[].type //字段类型
 * @param data.update.actionFields[].cols[].fields[]._id //字段id
 * @returns {*}
 */
export const udpateBusinessStepAction = ({commit, state, rootState, dispatch}, data) => {
  return businessConfig.udpateBusinessStepAction(data)
    .then(response => {
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 删除步骤动作
 * @param data
 * @param data._id  //步骤id
 * @param data.actionId  //动作id
 * @returns {*}
 */
export const deleteBusinessStepAction = ({commit, state, rootState, dispatch}, data) => {
  return businessConfig.deleteBusinessStepAction(data)
    .then(response => {
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 复制步骤动作
 * @param data
 * @param data._id  //步骤id
 * @param data.actionId  //动作id
 * @returns {*}
 */
export const copyBusinessStepAction = ({commit, state, rootState, dispatch}, data) => {
  return businessConfig.copyBusinessStepAction(data)
    .then(response => {
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 添加和修改字段
 * @param data
 * @param data._id  // 字段id 不传为添加字段
 * @param data.name  //字段名称
 * @param data.required  //是否必填
 * @param data.enable  //是否启用
 * @param data.show  //是否展示到卡片
 * @param data.type  //字段类型
 * @param data.isReport  //报表展示（暂无功能）
 * @param data.default  //默认值
 * @param data.flow_id  //工单id
 *@returns {*}
 */
export const addBusinessFlowFields = ({commit, state, rootState, dispatch}, data) => {
  return businessConfig.addBusinessFlowFields(data)
    .then(response => {
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 查询流程的所有字段
 * @param data
 * @param data.query  //名称
 * @param data.flow_id  //流程id
 * @param data.limit
 * @param data.page
 * @returns {*}
 */
export const queryBusinessFlowFields = ({commit, state, rootState, dispatch}, data) => {
  return businessConfig.queryBusinessFlowFields(data)
    .then(response => {
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 新版工单对接url保存
 * @param data
 * @param data._id  //流程id
 * @param data.dockingUrl // 对接项
 * @returns {*}
 */
export const saveBusinessDockingConfiguration = ({commit, state, rootState, dispatch}, data) => {
  return businessConfig.saveBusinessDockingConfiguration(data)
    .then(response => {
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 查询所在步骤的触发器
 * @param data
 * @param data.stepId  //步骤id
 * @returns {*}
 */
export const queryBusinessTrigger = ({commit, state, rootState, dispatch}, data) => {
  return businessConfig.queryBusinessTrigger(data)
    .then(response => {
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 添加触发器
 * @param data
 * @param data.stepId //动作id
 * @param data.title //名称
 * @param data.desc //描述
 * @param data.eventType //事件类型
 * @param data.all //全部条件满足
 * @param data.any //任一条件满足
 * @param data.actions //操作
 * @returns {*}
 */
export const addBusinessTrigger = ({commit, state, rootState, dispatch}, data) => {
  return businessConfig.addBusinessTrigger(data)
    .then(response => {
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 触发器启用，停用及排序
 * @param data
 * @param data.stepId //步骤id
 * @param data.triggerId //触发器id，启用停用时传
 * @param data.actionType //（run/stop/sort）
 * @param data.sortList //排序触发器id的数组，actionType==sort时才存在此字段
 * @returns {*}
 */
export const controlBusinessTrigger = ({commit, state, rootState, dispatch}, data) => {
  return businessConfig.controlBusinessTrigger(data)
    .then(response => {
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 编辑触发器
 * @param data
 * @param data.triggerId //触发器id
 * @param data.stepId //动作id
 * @param data.title //名称
 * @param data.desc //描述
 * @param data.eventType //事件类型
 * @param data.all //全部条件满足
 * @param data.any //任一条件满足
 * @param data.actions //操作
 * @returns {*}
 */
export const updateBusinessTrigger = ({commit, state, rootState, dispatch}, data) => {
  return businessConfig.updateBusinessTrigger(data)
    .then(response => {
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 查询账户内所有触发器触发器
 * @param data // 空对象
 * @returns {*}
 */
export const queryAccountBusinessTrigger = ({commit, state, rootState, dispatch}, data) => {
  return businessConfig.queryAccountBusinessTrigger({})
    .then(response => {
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 复制工单模版
 * @param data
 * @param data._id  //工单模版id
 * @param data.name // 工单模版名称
 * @returns {*}
 */
export const copyBusinessFlow = ({commit, state, rootState, dispatch}, data) => {
  return businessConfig.copyBusinessFlow(data)
    .then(response => {
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 查询工单满意度
 * @param data
 * @returns {*}
 */
export const getAppraiseTemplates = ({commit, state, rootState, dispatch}, data) => {
  return businessConfig.getAppraiseTemplates(data)
    .then(response => {
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 添加修改工单满意度
 * @param data
 * @returns {*}
 */
export const saveAppraiseTemplate = ({commit, state, rootState, dispatch}, data) => {
  return businessConfig.saveAppraiseTemplate(data)
    .then(response => {
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 查询工单信息对接配置
 * @param data
 * @returns {*}
 */
export const queryBusinessInfoPushConfig = ({commit, state, rootState, dispatch}, data) => {
  return businessConfig.queryBusinessInfoPushConfig(data)
    .then(response => {
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 保存工单信息对接配置
 * @param data
 * @returns {*}
 */
export const updateBusinessInfoPushConfig = ({commit, state, rootState, dispatch}, data) => {
  return businessConfig.updateBusinessInfoPushConfig(data)
    .then(response => {
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}